package com.zj.oauth2client.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.zj.oauth2client.dta.UmsAdminLoginParam;
import com.zj.oauth2core.common.api.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@RestController
public class IndexController {
    @Autowired
    private UserController userController;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private EurekaClient client;

    @GetMapping("/")
    public Object index(Authentication authentication) {
        return userController.getCurrentUser(authentication);
    }
    @GetMapping(value = "/client/login")
    public Object clientLogin(){
        InstanceInfo instanceInfo = client.getNextServerFromEureka("authorization-server", false);
        UmsAdminLoginParam umsAdminLoginParam = new UmsAdminLoginParam();
        umsAdminLoginParam.setPassword("admin");
        umsAdminLoginParam.setUsername("admin");
        String url = instanceInfo.getHomePageUrl() + "admin/login";
        ResponseEntity<CommonResult> result = restTemplate.postForEntity(url,umsAdminLoginParam,CommonResult.class);
        return result;
    }
}
